//
//  LGTopicCell.m
//  lgdw
//
//  Created by mac on 16/1/19.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "LGTopicCell.h"
#import "LGUnderwayView.h"
#import "LGEndView.h"
#import "LGTopic.h"
#define Margin 10

@interface LGTopicCell ()
/** 大图 */
@property (nonatomic , strong) UIImageView *iconView;
/** 报名中进行中View */
@property (nonatomic , strong) LGUnderwayView *underwayView;
/** 报名结束的View */
@property (nonatomic , strong) LGEndView *endView;
/** 标题 */
@property (nonatomic , strong) UILabel *titleLabel;
/** 间隔 */
@property (nonatomic , strong) UIView *marginView;


@end

@implementation LGTopicCell

// 赋值
- (void)setTopicModel:(LGTopic *)topicModel {
    // 防止重用
    self.iconView.image = nil;
    self.titleLabel.text = nil;
    
    _topicModel = topicModel;
    
    // 大图
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:self.topicModel.image] placeholderImage:[UIImage imageNamed:@"placeholder_image"]];
    // 标题
    self.titleLabel.text = self.topicModel.title;
    
    self.underwayView.topicModel = topicModel;
    
    if (topicModel.status == 1) {
        self.underwayView.hidden = NO;
        self.endView.hidden = YES;
    } else {
        self.endView.hidden = NO;
        self.underwayView.hidden = YES;
    }
    
}


#pragma -mark /***************** 重写init *****************/
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    return self;
}

#pragma -mark /***************** 设置界面 *****************/
- (void)setupUI {
    
    // 添加控件
    [self addSubview:self.iconView];
    [self.iconView addSubview:self.underwayView];
    [self addSubview:self.endView];
    [self addSubview:self.titleLabel];
    [self addSubview:self.marginView];
    
    // 布局控件
    // 大图
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_top);
        make.left.mas_equalTo(self.mas_left);
        make.right.mas_equalTo(self.mas_right);
        CGFloat cellHeight = [UIScreen mainScreen].bounds.size.width * 0.6;
        make.height.mas_equalTo(cellHeight);
    }];
    // 进行
    [self.underwayView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.iconView.mas_bottom);
        make.left.mas_equalTo(self.mas_left);
        make.right.mas_equalTo(self.iconView.mas_right);
        make.height.mas_equalTo(30);
    }];
    [self.underwayView underwayViewWithMax:self.topicModel.member_max Num:self.topicModel.member_num];
    
    // 结束
    [self.endView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.iconView.mas_bottom);
        make.left.mas_equalTo(self.mas_left);
        make.right.mas_equalTo(self.iconView.mas_right);
        make.height.mas_equalTo(30);
    }];
    
    // 标题
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.iconView.mas_bottom).offset(Margin / 2);
        make.left.mas_equalTo(self.mas_left).offset(Margin);
        make.right.mas_equalTo(self.mas_right).offset(-Margin);
    }];
    // 间隔
    [self.marginView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(Margin);
        make.left.mas_equalTo(self.mas_left);
        make.right.mas_equalTo(self.mas_right);
        make.bottom.mas_equalTo(self.mas_bottom);
    }];
}


#pragma -mark /***************** 懒加载控件 *****************/
// 大图
- (UIImageView *)iconView {
    if (_iconView == nil) {
        _iconView = [[UIImageView alloc] initAndClipsToBounds];
        _iconView.image = [UIImage imageNamed:@"placeholder_image"];
    }
    return _iconView;
}
// 报名进行中的View
- (UIView *)underwayView {
    if (_underwayView == nil) {
        _underwayView = [[LGUnderwayView alloc] init];
    }
    return _underwayView;
}
// 报名结束的View
- (UIView *)endView {
    if (_endView == nil) {
        _endView = [[LGEndView alloc] init];
        //        _endView.backgroundColor = [UIColor redColor];
    }
    return _endView;
}
// 标题lable
- (UILabel *)titleLabel {
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] initWithTitle:@"阿附近的辣弗拉解放啦椒粉多拉风微软风科技的" FontSize:14 TextColor:nil];
        _titleLabel.numberOfLines = 2;
    }
    return _titleLabel;
}
// 间隔
- (UIView *)marginView {
    if (_marginView == nil) {
        _marginView = [[UIView alloc] init];
        _marginView.backgroundColor = [[UIColor alloc] initWithWhite:0.95 alpha:1.0];
    }
    return _marginView;
}
@end
